﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Common;
using Nova_DAL;

public partial class Storage_UpdateStorage : System.Web.UI.Page
{
    Nova_DAL.DbHelper access = new DbHelper();
    string strSQL = "";
    DbCommand cmd = null;
    string strBaoJin = "";
    string strPID = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidden.Value))
        {
            bind();
            hidden.Value = "";
        }
        if (!IsPostBack)
        {
            Nova_Common.LoginLogic.IsLogin();
            bind();
            BindDDL();
        }
    }

    protected void bind()
    {
        cmd = access.GetSqlStringCommond(getSQL());
        gv.DataSource = access.ExecuteDataSet(cmd);
        gv.DataBind();
        GroupRows(gv,2);
    }

    private void BindDDL()
    {
        strSQL =@"SELECT TBP.Pos_ID,TBZ.Zone_Name  FROM TBase_Position TBP LEFT JOIN TBase_Zone TBZ ON TBP.Zone_ID = TBZ.Zone_ID WHERE TBZ.Structure=2
                    and ifDis=0";
        cmd=access.GetSqlStringCommond(strSQL);
        ddlZoneID.DataTextField = "Zone_Name";
        ddlZoneID.DataValueField = "Pos_ID";
        ddlZoneID.DataSource = access.ExecuteDataSet(cmd);
        ddlZoneID.DataBind();
        ddlZoneID.Items.Insert(0, new ListItem("自动化立体库", "0"));
        ddlZoneID.Items.Insert(0, new ListItem("请选择库区...", "1")); //由于插入的索引值每次为0，前面插入的索引值依次加1
    }

    public override void VerifyRenderingInServerForm(Control control)
    { }
    protected string getSQL()
    {
        HttpCookie WType = Request.Cookies["WType"];

        strSQL = @"Select  TBP.Pos_ID,TBS.PID, TBS.Mat_Num,TBM.Mat_Name,TBM.Unit_Base,TBM.MiniNum,TBM.Mat_Code,TBI.TS01,TBS.CDT,TRP.Tray_ID,TBM.S01  
                   From   TBase_Storage TBS 
               LEFT JOIN TRel_TrayPosContainer TRT ON TBS.ContainerID=TRT.ContainerID 
               LEFT JOIN TBase_TrayPos TBT ON TBT.Relations_ID = TRT.Relations_ID 
               LEFT JOIN TRelations TRP ON TRP.Tray_ID = TBT.Tray_ID 
               LEFT JOIN TBase_Position TBP ON TRP.Pos_ID = TBP.Pos_ID 
               LEFT JOIN TBase_InterFace TBI ON TBI.Sec_Mat_ID =TBS.Sec_Mat_ID 
               LEFT JOIN TBase_Material TBM ON TBM.Mat_Code = TBI.Mat_Code 
               LEFT JOIN TBase_Zone TBZ ON TBP.Zone_ID = TBZ.Zone_ID
                Where 1=1 AND (TBI.S01 = '" + WType.Value.ToString().Trim() + "' OR 'FSSC'='" + WType.Value.ToString().Trim() + "')";

        //        strSQL = @"Select  TBP.Pos_ID,TBS.PID, TBS.Mat_Num,TBM.Mat_Name,TBM.Unit_Base,TBM.TM10,TBM.Mat_Code,TBS.CDT,TRP.Tray_ID  
        //                   From   TBase_Storage TBS 
        //               LEFT JOIN TRel_TrayPosContainer TRT ON TBS.ContainerID=TRT.ContainerID 
        //               LEFT JOIN TBase_TrayPos TBT ON TBT.Relations_ID = TRT.Relations_ID 
        //               LEFT JOIN TRelations TRP ON TRP.Tray_ID = TBT.Tray_ID 
        //               LEFT JOIN TBase_Position TBP ON TRP.Pos_ID = TBP.Pos_ID 
        //               LEFT JOIN TBase_InterFace TBI ON TBI.Sec_Mat_ID =TBS.Sec_Mat_ID 
        //               LEFT JOIN TBase_Material TBM ON TBM.Mat_Code = TBI.Mat_Code 
        //               LEFT JOIN TBase_Zone TBZ ON TBP.Zone_ID = TBZ.Zone_ID
        //                Where 1=1 ";

        if (txtMat_Code.Text != "")
        {
            strSQL += " and TBM.Mat_Code like '%" + txtMat_Code.Text.ToString().Trim() + "%'";
        }
        if (Position1.No_Lane != "0" && Position1.No_Lane != "")
        {
            strSQL += " AND TBP.No_Lane = '" + Position1.No_Lane.ToString() + "'";
        }
        if (Position1.No_Line != "0" && Position1.No_Line != "")
        {
            strSQL += " AND TBP.No_Line = '" + Position1.No_Line.ToString() + "'";
        }

        if (Position1.No_Col != "0" && Position1.No_Col != "")
        {
            strSQL += " AND TBP.No_Col = '" + Position1.No_Col.ToString() + "'";
        }
        if (Position1.No_Layer != "0" && Position1.No_Layer != "")
        {
            strSQL += " AND TBP.No_Layer = '" + Position1.No_Layer.ToString() + "'";
        }
        if (txtTray_ID.Text != "")
        {
            strSQL += " AND TRP.Tray_ID like '%" + txtTray_ID.Text.ToString() + "%'";
        }
        if (ddlZoneID.SelectedIndex > 1)
        {
            strSQL += " AND TBP.Pos_ID='" + ddlZoneID.SelectedValue.ToString() + "'";
        }
        else if (ddlZoneID.SelectedIndex == 1)
        {
            strSQL += " AND TBZ.Structure=1 and TBZ.ifDis=0";
        }
        strSQL += " and TBP.Pos_ID != '200000000000' Order By  TRP.Tray_ID, TBS.CDT DESC";
        return strSQL;
    }

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        bind();
    }

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl_TM10 = (Label)e.Row.FindControl("lblTM10");
            if (lbl_TM10.Text == "")
            {
                strBaoJin = "0";
            }
            else
            {
                strBaoJin = lbl_TM10.Text.ToString();
            }

            if (Convert.ToDecimal(e.Row.Cells[5].Text.ToString()) <= Convert.ToDecimal(strBaoJin))
            {
               // e.Row.BackColor = System.Drawing.Color.FromName("#ff0000");
              //  e.Row.Cells[5].BackColor = System.Drawing.Color.Red;
            }

            LinkButton lbtn_PID = (LinkButton)e.Row.FindControl("lbtn_PID");
            string str = string.Format("javascript:document.getElementById('{1}').value=window.open('UpdatePOP.aspx?Ram=" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "&Mode={0}&PID={1}', window, 'Height=220px,Width=600px,top=250, left=500, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');document.URL=location.href;void(0);",
               "update",gv.DataKeys[e.Row.RowIndex].Value.ToString(), hidden.ClientID);
            lbtn_PID.Attributes.Add("onclick", str);
        }
    }
    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        this.gv.PageIndex = e.NewPageIndex;
        this.bind();
    }

    public static void GroupRows(GridView GridView1, int cellNum)
    {
        int i = 0, rowSpanNum = 1;
        while (i < GridView1.Rows.Count - 1)
        {
            GridViewRow gvr = GridView1.Rows[i];

            for (++i; i < GridView1.Rows.Count; i++)
            {
                GridViewRow gvrNext = GridView1.Rows[i];
                if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
                {
                    gvrNext.Cells[cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
                    break;
                }

                if (i == GridView1.Rows.Count - 1)
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    }
}